home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1994 March / Internet Info CD-ROM (Walnut Creek) (March 1994).iso / networking / osi / isode / dosisode / DOSISODE80.ZIP / ISODE8.WRK / UNIX / LIB / DNS / GETNETEN.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-16  |  2.8 KB  |  123 lines

  1. #include <fiddle.h>
  2. #define _PATH_NETWORKS "/etc/networks"
  3. /*
  4.  * Copyright (c) 1983 Regents of the University of California.
  5.  * All rights reserved.
  6.  *
  7.  * Redistribution and use in source and binary forms are permitted
  8.  * provided that: (1) source distributions retain this entire copyright
  9.  * notice and comment, and (2) distributions including binaries display
  10.  * the following acknowledgement:  ``This product includes software
  11.  * developed by the University of California, Berkeley and its contributors''
  12.  * in the documentation or other materials provided with the distribution
  13.  * and in all advertising materials mentioning features or use of this
  14.  * software. Neither the name of the University nor the names of its
  15.  * contributors may be used to endorse or promote products derived
  16.  * from this software without specific prior written permission.
  17.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  18.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  19.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  20.  */
  21.  
  22. #if defined(LIBC_SCCS) && !defined(lint)
  23. static char sccsid[] = "@(#)getnetent.c    5.7 (Berkeley) 6/1/90";
  24. #endif /* LIBC_SCCS and not lint */
  25.  
  26. #include <stdio.h>
  27. #include <sys/types.h>
  28. #include <sys/socket.h>
  29. #include <netdb.h>
  30. #include <ctype.h>
  31.  
  32. #define    MAXALIASES    35
  33.  
  34. static FILE *netf = NULL;
  35. static char line[BUFSIZ+1];
  36. static struct netent net;
  37. static char *net_aliases[MAXALIASES];
  38. int _net_stayopen;
  39. static char *any();
  40.  
  41. setnetent(f)
  42.     int f;
  43. {
  44.     if (netf == NULL)
  45.         netf = fopen(_PATH_NETWORKS, "r" );
  46.     else
  47.         rewind(netf);
  48.     _net_stayopen |= f;
  49. }
  50.  
  51. endnetent()
  52. {
  53.     if (netf) {
  54.         fclose(netf);
  55.         netf = NULL;
  56.     }
  57.     _net_stayopen = 0;
  58. }
  59.  
  60. struct netent *
  61. getnetent()
  62. {
  63.     char *p;
  64.     register char *cp, **q;
  65.  
  66.     if (netf == NULL && (netf = fopen(_PATH_NETWORKS, "r" )) == NULL)
  67.         return (NULL);
  68. again:
  69.     p = fgets(line, BUFSIZ, netf);
  70.     if (p == NULL)
  71.         return (NULL);
  72.     if (*p == '#')
  73.         goto again;
  74.     cp = any(p, "#\n");
  75.     if (cp == NULL)
  76.         goto again;
  77.     *cp = '\0';
  78.     net.n_name = p;
  79.     cp = any(p, " \t");
  80.     if (cp == NULL)
  81.         goto again;
  82.     *cp++ = '\0';
  83.     while (*cp == ' ' || *cp == '\t')
  84.         cp++;
  85.     p = any(cp, " \t");
  86.     if (p != NULL)
  87.         *p++ = '\0';
  88.     net.n_net = inet_network(cp);
  89.     net.n_addrtype = AF_INET;
  90.     q = net.n_aliases = net_aliases;
  91.     if (p != NULL)
  92.         cp = p;
  93.     while (cp && *cp) {
  94.         if (*cp == ' ' || *cp == '\t') {
  95.             cp++;
  96.             continue;
  97.         }
  98.         if (q < &net_aliases[MAXALIASES - 1])
  99.             *q++ = cp;
  100.         cp = any(cp, " \t");
  101.         if (cp != NULL)
  102.             *cp++ = '\0';
  103.     }
  104.     *q = NULL;
  105.     return (&net);
  106. }
  107.  
  108. static char *
  109. any(cp, match)
  110.     register char *cp;
  111.     char *match;
  112. {
  113.     register char *mp, c;
  114.  
  115.     while (c = *cp) {
  116.         for (mp = match; *mp; mp++)
  117.             if (*mp == c)
  118.                 return (cp);
  119.         cp++;
  120.     }
  121.     return ((char *)0);
  122. }
  123.